/**
 * Namespace for jQuery
 * @name $
 * @namespace
 */

(function($, gmwk) {

    gmwk.utils = function() {
          
        /**
         * Convert the output of serializeArray to key = value.
         *    http://stackoverflow.com/questions/1184624/serialize-form-to-json-with-jquery
         * @member $
         * @public
         */
        $.fn.serializeObject = function() {
            var Obj = {};
            var aSerialized = this.serializeArray();
            $.each(
                aSerialized,
                function() {
                    if (Obj[this.name]) {
                        if (!Obj[this.name].push) {
                            Obj[this.name] = [Obj[this.name]];
                        }
                        Obj[this.name].push(this.value || '');
                    } else {
                        Obj[this.name] = this.value || '';
                    }
                }
            );
            return Obj;
        };
      
        /**
         * Determine object length as the number of properties, non recursive.
         *    TODO: Extend to count all properties recursively
         * @public
         */
        gmwk.objectLength = function(Obj) {
            var nSize = 0;
            for (var sKey in Obj) {
                nSize++;
            }
            return nSize;    
        }

        /**
         * Compare two objects.
         *    TODO: Does not support nested objects or arrays
         *    TODO: Could be more efficient?
         * @param {Object} Obj1
         * @param {Object} Obj2
         * @public
         * @returns {Boolean} True if objects have exact same properties
         */
        gmwk.compareObjects = function(Obj1, Obj2) {
            var nLen1 = gmwk.objectLength(Obj1);
            var nLen2 = gmwk.objectLength(Obj2);
            if (nLen1 != nLen2) {
                return false;
            }
            for (key in Obj1) {
                if (Obj1[key] !== Obj2[key]) {
                    return false;
                }
            }
            return true;
        }
      
    }

})(jQuery, window.gmwk);
